 package org.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

 public class DBTest {
    public static void main(String[] args) throws ClassNotFoundException,java.sql.SQLException{


                // 用 Java 连接MySQL

                // 1. 注册驱动
      //  Class.forName("com.mysql.jdbc.Driver");
                Class.forName("com.mysql.cj.jdbc.Driver");

                // 2. 建立连接
                String url = "jdbc:mysql://localhost:3306/student?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";
                Connection connection = DriverManager.getConnection(url, "root", "520235");

                // 3. 执行SQL语句
                String sql = "select * from user ";
                Statement statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery(sql);



                // 4. 处理结果集
                while (resultSet.next()){
                    String userid = resultSet.getString("Userid");
//                    String No = resultSet.getString(1);
                    String sname = resultSet.getString("Sname");

                    System.out.println(sname + " : " + userid + " " );
                }

                // 5. 关闭连接
                resultSet.close();
                statement.close();
                connection.close();




    }
}